layui.use(['jquery', "element", 'layer'], function () {
    var $ = layui.jquery,
        element = layui.element;
    $(function () {
        FastClick.attach(document.body);
        setMenu();
        $(".left-menu").on("click", "li", function () {
            if (!$(this).data("ignore")) {
                $(this).siblings().removeClass("layui-nav-itemed");
            }
        });
        $(".left-menu").find("a[data-id]").click(function () {
            sessionStorage.dataId = $(this).attr('data-id');
            sessionStorage.parentId = $(this).attr('data-parentid');
        });
        $(".menu-flexible").click(function () {
            $(".left-menu-all").toggle();
            $(".layui-body,.layui-footer").css("left", ($(".left-menu-all").is(":hidden")) ? "0" : "200px");
        });
        $(document).on("click", ".mulu,.masked", function () {
            $('body').toggleClass('mulu-hide');
        });
        $(document).on('click', '.layui-table .layui-table-first', function () {
            if ($(window).width() > 992) {
                return;
            }
            if ($(this).hasClass('cur')) {
                $(this).siblings().hide();
                $(this).removeClass('cur');
            } else {
                $($(this).siblings()).css('display', '-webkit-box');
                $(this).addClass('cur');
            }
        });
        /*退出系统*/
        $(".btn_quit").on("click", function () {
            $.ajax({
                type: "POST",
                url: "/administrate/gate/logout",
                async: false,
                dataType: "json",
                success: function (result) {
                    if (result.code == 200) {
                        sessionStorage.clear();
                        location.href = location.href;
                    }
                }
            });
        });

        /*设置菜单*/
        function setMenu() {
            var array = new Array();
            if (!sessionStorage.getItem('menuArray')) {
                $.ajax({
                    type: "GET",
                    url: "/administrate/ownmenus",
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            $.each(result.menus, function (idx, obj) {
                                if (obj.parentid == null) {
                                    obj.children = menuConvertTree(result.menus, obj.id);
                                    array.push(obj);
                                }
                            });
                            menuSort(array);
                            sessionStorage.menuArray = JSON.stringify(array);
                        }
                    }
                });
            }
            else {
                array = JSON.parse(sessionStorage.getItem('menuArray'));
            }
            $("ul[lay-filter='left-menu']").html(menuRecombineHtml(array));
            element.init();
        }

        /*菜单转换成树状结构*/
        function menuConvertTree(data, id) {
            var fa = function (parentid) {
                var _array = [];
                for (var i = 0; i < data.length; i++) {
                    var n = data[i];
                    if (n.parentid === parentid) {
                        n.children = fa(n.id);
                        _array.push(n);
                    }
                }
                return _array;
            };
            return fa(id);
        }

        /*菜单排序,正序*/
        function menuSort(data) {
            for (var i = 0; i < data.length; i++) {
                menuSort(data[i].children)
            }
            data.sort(function (a, b) {
                return a.sort > b.sort;
            });
        }

        /*重组菜单HTML*/
        function menuRecombineHtml(array) {
            var html = "";
            html += "<li class=\"layui-nav-item " + (sessionStorage.getItem('dataId') == "index" ? "layui-this" : "" ) + "\"><a href=\"/administrate\" data-id=\"index\">后台首页</a></li>";
            $.each(array, function (idx, obj) {
                if (sessionStorage.getItem('parentId') == obj.id) {
                    html += "<li class=\"layui-nav-item layui-nav-itemed\">";
                }
                else {
                    html += "<li class=\"layui-nav-item\">";
                }
                var links = obj.links != "" && obj.links != null && obj.children.length <= 0 ? obj.links : "javascript:;";
                html += "<a href=\"" + links + "\" data-parentid=\"" + obj.id + "\" data-id=\"" + obj.id + "\">" + obj.name + "</a>";
                if (obj.children.length > 0) {
                    html += "<dl class=\"layui-nav-child\">";
                    $.each(obj.children, function (idx, chirld) {
                        if (sessionStorage.getItem('dataId') == chirld.id) {
                            html += "<dd class=\"layui-this\">";
                        } else {
                            html += "<dd>";
                        }
                        html += "<a href=\"" + chirld.links + "\" data-parentid=\"" + chirld.parentid + "\" data-id=\"" + chirld.id + "\">" + chirld.name + "</a>";
                        html += "</dd>";
                    });
                    html += "</dl>";
                }
                html += "</li>";
            });
            html += "<li class=\"layui-nav-item\" data-ignore='true'><a href=\"/DruidMonitor/index.html\" target='_blank'>SQL监控</a></li>";
            return html;
        }

    });
});

/*时间格式化*/
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}